/*
 * @Author: silas 
 * @Date: 2021-08-25 15:15:03 
 * @Last Modified by: silas
 * @Last Modified time: 2021-08-25 16:42:43
 */
<template>
    <div class="tobBtoC">
        <div>
            <el-form
                :inline="true"
                :model="form"
                label-width="60px"
                class="formList"
            >
                <el-form-item label="账期" style="width: 15%">
                    <el-date-picker
                        style="width: 140px"
                        v-model="form.time"
                        type="year"
                        placeholder="选择账期"
                        value-format="yyyy"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item style="width: 30%">
                    <el-button
                        style="margin-left: 20px"
                        size="mini"
                        type="primary"
                        @click="handle_search"
                        >查询</el-button
                    >
                    <el-button @click="handle_down()" size="mini"
                        >模板下载</el-button
                    >
                    <el-button
                        type="orange"
                        @click="
                            dialogVisible = true;
                            importForm.year = '';
                            importForm.description = '';
                            importForm.title = '';
                            fileList = [];
                        "
                        size="mini"
                        >导入</el-button
                    >
                </el-form-item>
            </el-form>
        </div>
        <div class="table-content clearfix">
            <div class="content">
                <div class="tableBox">
                   <el-table
                id="table1"
                ref="table"
                size="mini"
                :header-cell-style="{ background: '#eef1f6' }"
                :data="tableData"
                v-loading="tableLoading"
                style="width: 100%"
                border
            >
                <el-table-column prop="testYear" label="账期" width="80" />
                <el-table-column
                    prop="title"
                    label="标题"
                    width="120"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="importUserName"
                    label="导入人"
                    width="100"
                />
                <el-table-column
                    prop="importDate"
                    label="导入时间"
                    width="140"
                />
                <el-table-column
                    prop="description"
                    label="说明"
                    min-width="140"
                    show-overflow-tooltip
                />
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="mini"
                            @click="handle_detail(scope.row)"
                            >详情</el-button
                        >
                        <el-button
                            type="text"
                            size="mini"
                            @click="handle_del(scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
                </div>
            </div>
        </div>
        <el-dialog
            title="导入文件"
            append-to-body
            :visible.sync="dialogVisible"
            width="450px"
        >
            <el-form :model="importForm" ref="importForm" :rules="rules">
                <el-form-item label="账期" prop="year" label-width="80px">
                    <el-date-picker
                        v-model="importForm.year"
                        type="year"
                        size="mini"
                        placeholder="选择账期"
                        value-format="yyyy"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label-width="80px" label="标题" prop="title">
                    <el-input
                        v-model="importForm.title"
                        autocomplete="off"
                        placeholder="请输入标题"
                        maxlength="50"
                    ></el-input>
                </el-form-item>
                <el-form-item
                    label-width="80px"
                    label="下达说明"
                    prop="description"
                >
                    <el-input
                        size="medium"
                        v-model="importForm.description"
                        autocomplete="off"
                        placeholder="请输入说明"
                        maxlength="200"
                        type="textarea"
                        resize="none"
                    ></el-input>
                </el-form-item>
                <el-form-item label="上传文件">
                    <el-upload
                        :limit="1"
                        accept=".xls, .xlsx"
                        class="avatar-uploader"
                        :auto-upload="false"
                        :file-list="fileList"
                        :on-change="changeFile"
                        action
                    >
                        <el-button size="mini" type="primary"
                            >点击上传</el-button
                        >
                    </el-upload>
                </el-form-item>
            </el-form>

            <div slot="footer">
                <div class="dialog-footer">
                    <el-button
                        size="mini"
                        type="primary"
                        @click="handle_import()"
                        >确定</el-button
                    >
                    <el-button size="mini" @click="dialogVisible = false"
                        >取消</el-button
                    >
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import prefixAPI from "@/api/prefixAPI";
import { getCookie } from "@/common/js/cookie";
import { getSessionStorage } from "@/common/js/storage";
import { $budgetAPI } from "@/api";
export default {
    name: "sacle",
    data() {
        return {
            form: {
                time: this.moment().format('yyyyMM'),
            },
            tableData: [],
            tableLoading: false,
            importForm: {
                year: "",
                title: "",
                description: "",
            },
            dialogVisible: false,
            fileList: [],
            rules: {
                year: [
                    { required: true, message: "请选择账期", trigger: "blur" },
                ],
                title: [
                    { required: true, message: "请输入标题", trigger: "blur" },
                ],
                description: [
                    { required: true, message: "请输入说明", trigger: "blur" },
                ],
            },
            param: {},
        };
    },
    methods: {
        init() {
            let that = this;

            that.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
            that.form.areaId = that.accountInfo.areaId;
            that.form.time = that.moment().format("YYYY");
            that.getData();
        },
        getData() {
            var that = this;

            that.tableLoading = true;
            $budgetAPI.getDeviceImportList(
                {
                    acctYear: that.form.time,
                },
                (res) => {
                    if (res && res.code == 0) {
                        that.tableData = res.content;
                    }
                    that.tableLoading = false;
                }
            );
        },
        handle_search() {
            this.getData();
        },
        handle_down() {
            var that = this,
                token = getCookie("token");

            window.location.href =
                prefixAPI.hp +
                "/api/netWorkDevice/downLoadNetWorkLineTemplate?acctMonth=" +
                "&token=" +
                token;
        },
        handle_detail(row) {
            // circle-tab2 
            this.$router.push({
                path: "/budget/deviceDetail",
                query: { id: row.id },
            });
        },
        handle_del(row) {
            var that = this;

            that.$confirm("是否删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                $budgetAPI.deleteDeviceByRecordId(
                    {
                        recordId: row.id,
                        token: getCookie("token"),
                    },
                    (res) => {
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!",
                            });
                        } else {
                            this.$message({
                                type: "error",
                                message: res.message,
                            });
                        }
                        that.getData();
                    }
                );
            });
        },
        // 批量导入上传文件传递数据
        changeFile(file, fileList) {
            this.fileList = fileList;
            this.param = new FormData();
            this.param.append("file", file.raw);
            // this.param.file = file.raw;
        },
        // 导入
        handle_import(type) {
            var that = this;

            that.$refs["importForm"].validate((valid) => {
                if (valid) {
                    if (that.fileList.length == 0) {
                        that.$message({
                            message: "请选择需要上传的文件",
                            type: "warning",
                        });
                        return false;
                    }
                    that.param.append(
                        "description",
                        that.importForm.description
                    );
                    that.param.append("token", getCookie("token"));
                    that.param.append("acctYear", that.importForm.year);
                    that.param.append("title", that.importForm.title);
                    $budgetAPI.importDeviceAData(
                        that.param,
                        (res) => {
                            if (res.code == 0) {
                                that.$message({
                                    message: "导入成功",
                                    type: "success",
                                });
                                that.getData();
                            } else {
                                that.$message.error(res.message);
                            }
                            that.dialogVisible = false;
                        },
                        (err) => {
                            that.dialogVisible = false;
                        }
                    );
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.remark {
    padding-top: 5px;
    text-align: left;
    .attention {
        font-weight: bold;
    }
}
.table-content {
    .table-header {
        display: flex;
        justify-content: space-between;
        .table-title {
            font-size: 14px;
            font-weight: bold;
            height: 30px;
            line-height: 30px;
            span {
                display: inline-block;
                width: 5px;
                height: 5px;
                background-color: #418ee9;
                border-radius: 1000%;
                overflow: hidden;
                margin-right: 10px;
                margin-bottom: 3px;
            }
        }
    }
    .content {
        margin-top: 15px;
    }
}
</style>
